<?php
namespace app\payclass\controller;
use app\AdminRest;

use app\payclass\model\Active;
use app\payclass\model\Auth;
use app\payclass\model\LookRecord;
use app\payclass\model\Video;
use app\shop\model\AdminGoods;
use think\App;

use app\payclass\model\ClassList as model;

use think\facade\Db;


class AdminClass extends AdminRest
{

    protected $model;

    protected $video_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

        $this->video_model = new Video();

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:35
     * @功能说明:列表
     */
    public function classList(){

        $input = $this->_param;

        $dis[] = ['status','>',-1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        if(!empty($input['cate_id'])){

            $dis[] = ['cate_id','=',$input['cate_id']];

        }

        $data = $this->model->classList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-09 11:01
     * @功能说明:添加
     */
    public function classAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $res = $this->model->classAdd($input);

        $id  = $this->model->getLastInsID();

        return $this->success($id);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 13:28
     * @功能说明:详情
     */
    public function classInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->classInfo($dis);

        $goods_model = new AdminGoods();
        //商品名称
        $data['goods_name'] = $goods_model->where(['id'=>$data['goods_id']])->value('name');

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 13:29
     * @功能说明:编辑
     */
    public function classUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->classUpdate($dis,$input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-08 13:47
     * @功能说明:商品列表
     */
    public function goodsSelect(){

        $input = $this->_param;

        $goods_model = new AdminGoods();

        $dis[] = ['status','=',1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['is_member','=',0];

        if(!empty($input['title'])){

            $dis[] = ['name','like','%'.$input['title'].'%'];
        }

        $data = $goods_model->where($dis)->order('top desc,id desc')->paginate($input['limit'])->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-08 13:59
     * @功能说明:视频列表
     */
    public function videoList(){

        $input = $this->_param;

        $dis[] = ['status','>',-1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        if(!empty($input['class_id'])){

            $dis[] = ['class_id','=',$input['class_id']];

        }

        $data = $this->video_model->videoList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-08 14:00
     * @功能说明:添加视频
     */
    public function videoAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $data = $this->video_model->videoAdd($input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-08 14:01
     * @功能说明:编辑视频
     */
    public function videoUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $res = $this->video_model->videoUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-08 14:01
     * @功能说明:编辑视频
     */
    public function videoInfo(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $res = $this->video_model->videoInfo($dis);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-09 09:54
     * @功能说明:课程列表
     */
    public function classSelect(){

        $dis = [

            'uniacid' => $this->_uniacid,

            'status'  => 1
        ];

        $data = $this->model->where($dis)->field('id,title')->order('top')->select()->toArray();

        if(!empty($data)){

            $atv_model = new Active();

            $class_id  = $atv_model->where(['uniacid'=>$this->_uniacid])->where('status','>',-1)->column('class_id');

            foreach ($data as &$v){

                $v['is_atv'] = in_array($v['id'],$class_id)?1:0;

            }

        }

        return $this->success($data);

    }





}
